/**
 * 常用js封装
 */
var index;
var _table;
(function ($) {
$.extend({
    modal:{
        get:function(url,param,callback){
          $.ajax({
              type:'get',
              dataType:'json',
              data:param,
              url:url,
              beforeSend:function(){
                   index = layer.load(1, {
                      shade: [0.1,'#fff'] //0.1透明度的白色背景
                  });
              },
              success:callback,
              error:function(ret){
                  layer.msg('网络异常,请检查网络');
              }
          })
        },
        deletes:function(url,param){
            $.modal.post(url,param,function(ret){
                if(ret.code==200){
                    layer.msg(ret.msg,function(){
                        _table.bootstrapTable('refresh')
                    })
                }
            });
        },
        saveWindow:function(url,param){
            $.modal.post(url,param,function(ret){
                if(ret.code==200){
                    // 自动关闭 等待时间
                    layer.msg(ret.msg,function(){
                        //关闭编辑的弹出层
                        let index = parent.layer.getFrameIndex(window.name);
                        parent._table.bootstrapTable('refresh');
                        parent.layer.close(index);

                    });

                }
            });

        },
        post:function(url,param,callback){
            $.ajax({
                type:'post',
                dataType:'json',
                data:param,
                url:url,
                beforeSend:function(){
                    index = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                },
                success:function (ret) {
                    callback.call(this,ret);
                    layer.close(index);
                },
                error:function(ret){
                    layer.msg('网络异常,请检查网络');
                }
            })
        },

        open:function(title,url,width,height){
            index =layer.open({
                type: 2,
                anim:5,
                title: title,
                shadeClose: true,
                resize: true,
                shade: 0.8,
                area: [width, height],
                content: url
            });
        },
        openFull:function(title,url){
            // type:弹出层的类型
            // title:弹出层标题
            // area :用于定义弹出层的宽度和高度的
            let index = layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                shade: 0.8,
                content: url
            });
            layer.full(index);
        },
        initTable:function ($table,columns,url) {
            _table = $table.bootstrapTable({
                toolbar:"#toolbar",
                showRefresh:true,
                showToggle:true,
                showExport:true,
                locale: 'zh-CN',
                exportDataType: "all",//显示导出范围
                exportTypes: ['json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'],//导出格式
                exportOptions: {//导出设置
                    fileName: 'Tablexxx',//下载文件名称
                },
                showFullscreen:true,
                sortable:true,
                showColumns:true,
                showColumnsToggleAll:true,
                pagination: true,
                sidePagination: 'server',
                striped: true, //是否显示行间隔色
                pageNumber: 1, //初始化
                pageSize: 10,//单页记录数
                pageList: [10, 20],//可选择单页记录数
                formatLoadingMessage: function () {
                    return '<span style="font-size:5px">正在努力加载数据，请稍后</font>';
                },
                queryParamsType:'',
                queryParams: function (param) {
                    let $form = $("form");
                    let p = $("form").serialize();
                    return p+"&pageNumber="+param.pageNumber;
                },
                responseHandler: function (ret) {
                    var result = {
                        "total": ret.total,
                        "rows": ret.list
                    };
                    return result;
                },
                columns: columns,
                url: url
            });
        },
        initTreeTable:function (url,columns,idField,treeShowField,parentIdField) {
            _table= $table.bootstrapTable({
                url: url,
                showColumns: true,
                idField: idField,
                showFullscreen:true,
                sortable:true,
                locale: 'zh-CN',
                formatLoadingMessage: function () {
                    return '<span style="font-size:5px">正在努力加载数据，请稍后</font>';
                },
                buttonsAlign: "right",
                exportDataType: "all",//显示导出范围
                exportTypes: ['json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'],//导出格式
                exportOptions: {//导出设置
                    fileName: 'Tablexxx',//下载文件名称
                },
                toolbar:"#toolbar",
                showColumnsToggleAll:true,
                columns: columns,
                treeShowField: treeShowField,
                parentIdField: parentIdField,
                dataType:'json',
                onPostBody: function() {

                    $table.treegrid({
                        treeColumn: 1
                    })
                }
            })
        }

    }
})
})(jQuery)